package com.saturn.titan.core.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.security.utils.Digests;
import org.springside.modules.utils.DateProvider;
import org.springside.modules.utils.Encodes;

import com.saturn.titan.core.bean.Permission;
import com.saturn.titan.core.bean.Role;
import com.saturn.titan.core.bean.User;
import com.saturn.titan.core.exception.ServiceException;
import com.saturn.titan.core.repository.mybatis.PermissionDao;
import com.saturn.titan.core.repository.mybatis.RoleDao;
import com.saturn.titan.core.repository.mybatis.UserDao;


/**
 * 用户管理类. 
 * 
 * @author lwh
 */
@Component
@Transactional(readOnly = true)
public class AccountService {

	public static final String HASH_ALGORITHM = "SHA-1";
	public static final int HASH_INTERATIONS = 1024;
	private static final int SALT_SIZE = 8;

	private static Logger logger = LoggerFactory.getLogger(AccountService.class);

	@Autowired
	private UserDao userDao;
	
	@Autowired
	private RoleDao roleDao;
	
	@Autowired
	private PermissionDao permissionDao;
	
	private DateProvider dateProvider = DateProvider.DEFAULT;
	
	public User getUser(Long id) {
		return userDao.selectById(id);
	}

	public Role getRole(Long id) {
		return roleDao.selectById(id);
	}
	
	public Permission getPermission(Long id) {
		return permissionDao.selectById(id);
	}
	
	public User findUserByLoginName(String loginName) {
		return null;
	}
	
	@Transactional(readOnly = false)
	public void registerUser(User user) {
		entryptPassword(user);
		// user.setRoles("user");
		user.setRegisterDate(dateProvider.getDate());
		if(user.getActiveCode() == null) {
			user.setActiveCode("");
		}
		user.setStatus("enabled");
		userDao.insert(user);
	}
	
	@Transactional(readOnly = false)
	public void updateUser(User user) {
		userDao.update(user);
	}

	@Transactional(readOnly = false)
	public void deleteUser(Long id) {
		if (isSupervisor(id)) {
			logger.warn("操作员{}尝试删除超级管理员用户", getCurrentUserName());
			throw new ServiceException("不能删除超级管理员用户");
		}
		userDao.deleteById(id);
	}

	@Transactional(readOnly = false)
	public void addPermission(Permission permission) {
		permissionDao.insert(permission);
	}

	@Transactional(readOnly = false)
	public void updatePermission(Permission permission) {
		permissionDao.update(permission);
	}

	@Transactional(readOnly = false)
	public void deletePermission(Long id) {
		permissionDao.deleteById(id);
	}
	

	@Transactional(readOnly = false)
	public void addRole(Role role) {
		roleDao.insert(role);
	}

	@Transactional(readOnly = false)
	public void updateRole(Role role) {
		roleDao.update(role);
	}

	@Transactional(readOnly = false)
	public void deleteRole(Long id) {
		roleDao.deleteById(id);
	}
	
	/**
	 * 判断是否超级管理员.
	 */
	private boolean isSupervisor(Long id) {
		return id == 1;
	}

	/**
	 * 取出Shiro中的当前用户LoginName.
	 */
	private String getCurrentUserName() {
		//ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		//return user.loginName;
		return "";
	}

	/**
	 * 设定安全的密码，生成随机的salt并经过1024次 sha-1 hash
	 */
	private void entryptPassword(User user) {
		byte[] salt = Digests.generateSalt(SALT_SIZE);
		user.setSalt(Encodes.encodeHex(salt));
	}
	
	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Autowired
	public void setPermissionDao(PermissionDao permissionDao) {
		this.permissionDao = permissionDao;
	}
	
	@Autowired
	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public void setDateProvider(DateProvider dateProvider) {
		this.dateProvider = dateProvider;
	}

	@Transactional(readOnly = false)
	public void addUserRole(Long uid, Long rid) {
		roleDao.addUserRole(uid,rid);
	}

	@Transactional(readOnly = false)
	public void deleteUserRole(Long uid, Long rid) {
		roleDao.deleteUserRole(uid,rid);
	}
	
}
